home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snip9611.zip / DBLROUND.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  1KB  |  62 lines

  1. /* +++Date last modified: 06-Sep-1996 */
  2.  
  3. /*
  4. **  DBLROUND.C - Rounds a double to the nearest whole number
  5. **  public domain by Ross Cottrell
  6. */
  7.  
  8. #include <float.h>
  9. #include <limits.h>
  10. #include "snipmath.h"
  11.  
  12. #if defined(__ZTC__)
  13.  #include <fltenv.h>
  14.  #define SAVEROUND() \
  15.        int fersave = fegetround(); \
  16.        fesetround(FE_TONEAREST)
  17.  #define RESTOREROUND() \
  18.        fesetround(fersave)
  19. #else
  20.  #if !defined(FLT_ROUNDS) || (FLT_ROUNDS != 1)
  21.   #error FLT_ROUNDS needs to be defined to be 1
  22.  #endif
  23.  #define SAVEROUND()
  24.  #define RESTOREROUND()
  25. #endif
  26.  
  27. double dround(double x)
  28. {
  29.       Boolean_T flag;
  30.       static volatile double X;
  31.  
  32.       SAVEROUND();
  33.       if (True_ == (flag = (x < 0.0)))
  34.             X = -x;
  35.       else  X = x;
  36.       X += 1.0 / DBL_EPSILON;
  37.       X -= 1.0 / DBL_EPSILON;
  38.       RESTOREROUND();
  39.       return ((flag) ? -X : X);
  40. }
  41.  
  42. #ifdef TEST
  43.  
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46.  
  47. main(int argc, char *argv[])
  48. {
  49.       double val;
  50.       char *dummy;
  51.  
  52.       while (--argc)
  53.       {
  54.             val = strtod((const char *)(*(++argv)), &dummy);
  55.             printf("dround(%g) = ", val);
  56.             printf("%.12g\n", dround(val));
  57.       }
  58.       return EXIT_SUCCESS;
  59. }
  60.  
  61. #endif /* TEST */
  62.